package com.jdk.learn.java8.lamda;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.jdk.learn.java8.bo.Apple;
import jdk.management.resource.ResourceId;

import java.util.Arrays;
import java.util.Random;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 *
 * Lamda表达式
 * 定义
 * parameterList  -->  body
 * parameterList  -->  {body}
 *  参数列表      链接  主体
 *
 * 使用
 * ----------------------------
 * ()-> System.out.print("world")
 * x->System.out.print(x)
 * x->{
 *  System.out.print(x)
 *  }
 *
 * x->{
 *  return x*x;
 *  }
 *x->System.out.print(x)
 *
 * ------------------------------------
 * 类型推导
 *
 * ------------------------------------
 * 常用函数
 *      Function<T,R>   R apply(T)    传入参数，返回一个对象
 *          BiFunction<T, U, R>    R apply(T t, U u);   T 第一个入参 U 第二个入参 R返回对象类型
 *      Consumer<T>   void accept(T)   传入参数，无返回
 *          BiConsumer<T, U>  void accept(T t, U u);  传入两个参数  T ，U
 *      Predicate<T>   boolean test(T)  传入参数，返回boolean
 *          BiPredicate<T, U>  boolean test(T t, U u);  传入两个参数  T ，U  返回boolean结果
 *      Supplier<T>    T get()   直接获取T类型的对象
 *
 * -----------------------
 * @author bgt
 * @version V1.0
 * @since 2019-01-07 10:32
 */
public class LamdaLearn {
    //无参数的,无返回值
    private static void noParamListNoReturn(){
        Runnable r=()-> System.out.print("world");
    }
    //无参数的,无返回值
    private static void noParamListNoReturn2(){
        Runnable r=()-> {
            System.out.print("world");
        };
    }

    //无参数有返回值
    private static void noParamListHasReturn(){
        ResourceId resourceId = () ->  "100";
        System.out.println( resourceId.getName());
    }
    //无参数有返回值
    private static void noParamListHasReturn2(){
        ResourceId resourceId = () -> {
            return "100";
        };
        System.out.println( resourceId.getName());
    }
    //无参数有返回值
    private static void noParamListHasReturn3(){
        Supplier<Apple> appleSupplier=Apple::new;
        System.out.println(appleSupplier.get());
    }
    //有参数 无返回值
    private static void hasParamListNoReturn() {
        Consumer<String> tConsumer = x -> System.out.println(x);
        tConsumer.accept("hello world");
    }
    //有参数 无返回值
    private static void hasParamListNoReturn2() {
        BiConsumer<String, String> consumer = (x, y) -> System.out.println(x + "---" + y);
        consumer.accept("Hello", "java");

    }
    //有参数 有返回值
    private static void hasParamListHasReturn() {
        Predicate<Apple> ricky = (Apple a) -> a.getName().equals("ricky");
        ricky.apply(new Apple("ricky", 100l, Arrays.asList("苹果"+new Random().nextInt())));
    }
    //有参数 有返回值
    private static void hasParamListHasReturn2() {
        Function<Integer, Boolean> ftFunction = x -> x>100;
        System.out.println(ftFunction.apply(101));
    }
    //有参数 有返回值
    private static void hasParamListHasReturn3() {
        BiFunction<String,Long,Apple> biFunction=(x,y)->Apple.getInstance(x,y);
        System.out.println( biFunction.apply("ricky_bai", 140l));
    }


    public static void main(String[] args) {
        noParamListHasReturn();
        noParamListHasReturn3();
        hasParamListNoReturn2();
        hasParamListNoReturn();
//        hasParamListNoReturn2();
        hasParamListHasReturn();
    }
}
